#include "main.h"
#include "color.h"
struct user_set
{
    char name[10];
    char pwd[10];
};
struct user_set user_arr[20]; //结构体数组
client_msg client_save[10];   //临时保存从文件中取出来的账户密码
int index2 = 0;
int index1 = 0;
int client_sockets[MAX_CLIENTS];//全局数组储存客户的套接字
void *talk(void *arg) // 用线程接受客户端发来的消息
{
    int clifd = *((int *)arg);
    Msg data;
    while (1)
    {
        read(clifd, &data, sizeof(data));
        switch (data.type)
        {
        case 1:
            response_reg(clifd, data);
            break;
        case 2:
            response_login(clifd, data);
            break;
        case 3:
            response_solo_talk(clifd, data);
            break;
        case 4:
            response_everybod_talk(clifd, data);
            break;
        }
    }
}
int response_login(int fd, Msg data)//回应注册消息
{
    int ret = 0;
    FILE *file = fopen("./1.txt", "r");
    while (1)
    {
        if (fread(&user_arr[index1++], sizeof(user_arr[index1]), 1, file) < 1)
        {
            break;
        }
        index1++;
    }
    for (int i = 0; i < index1; i++)
    {
        if ((strcmp(user_arr[i].name, data.user_name) == 0) && (strcmp(user_arr[i].pwd, data.user_pwd) == 0))
        {
            ret = 1;
            client_save[index2].client_id = fd;
            strcpy(client_save[index2].client_name, data.user_name);
            printf("id=%d,name=%s\n", client_save[index2].client_id, client_save[index2].client_name);
            index2++;
            write(fd, &ret, sizeof(ret));
            return 1;
        }
    }
    write(fd, &ret, sizeof(ret));
    fclose(file);
    return 1;
}
void response_reg(int fd, Msg data)//回应登录消息
{
    int ret = 0;
    FILE *file = fopen("./1.txt", "r+");
    while (1)
    {
        if (fread(&user_arr[index1], sizeof(user_arr[index1]), 1, file) < 1)
        {
            break;
        }
        index1++;
    }
    for (int i = 0; i < index1; i++)
    {
        if (strcmp(user_arr[i].name, data.user_name) == 0)
        {
            write(fd, &ret, sizeof(int));
            return;
        }
    }
    strcpy(user_arr[index1].name, data.user_name);
    strcpy(user_arr[index1].pwd, data.user_pwd);
    fseek(file, 0, SEEK_END);
    fwrite(&user_arr[index1], sizeof(user_arr[index1]), 1, file);
    index1++;
    ret = 1;
    write(fd, &ret, sizeof(ret));
    fclose(file);
}
void response_everybod_talk(int fd, Msg talk)//回应群聊消息
{
    char buff[20] = {0};
    for (int i = 0; i < MAX_CLIENTS; i++)
    {
        if (strncmp(talk.word, "exit", 4) == 0)
        {
            break;
        }
        if (client_sockets[i] != 0 && client_sockets[i] != fd)
        {
            write(client_sockets[i], &talk, sizeof(Msg));
        }
    }
}
void send_online_clientid(int fd, Msg talk)//发送在线客户端ID
{
    char buff[10] = {0};
    write(fd, client_save, sizeof(client_save));
}
void response_solo_talk(int fd, Msg talk)//回应单聊消息
{

    char buff[20] = {0};
    int fd1 = 0;
    for (int i = 0; i < index2; i++)
    {

        if (strcmp(client_save[i].client_name, talk.user_name) == 0)
        {
            fd1 = client_save[i].client_id;
            break;
        }
    }
    for (int j = 0; j < index2; j++)
    {
        if (client_save[j].client_id == fd)
        {
            strcpy(talk.user_name, client_save[j].client_name);
            break;
        }
    }
    write(fd1, &talk, sizeof(Msg));
    if (strncmp(talk.word, "exit", 4) == 0)
    {
        return;
    }
}
int main()
{
    int serfd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(SER_PORT);
    addr.sin_addr.s_addr = inet_addr(SER_IP);
    bind(serfd, (struct sockaddr *)&addr, sizeof(addr));
    listen(serfd, 10);
    Msg data;
    int i = 0;
    int count = 0;
    int new_socket;
    printf("等待用户上线......\n");
    while (1)
    {
        new_socket = accept(serfd, NULL, NULL);
        if (new_socket == -1)
        {
            printf(" recv client connect failed\n");
            return -4;
        }
        printf("用户%d上线了~\n", new_socket);
        client_sockets[count] = new_socket;
        count++;
        pthread_t tid;
        pthread_create(&tid, NULL, talk, (void *)&new_socket);
        pthread_detach(tid);
    }
    for (i = 0; i < MAX_CLIENTS; i++)
    {
        close(client_sockets[i]);
    }

    close(serfd);
}
